using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using TCDbService;
using TCEntity;

namespace TimeCameraServer.Controllers
{
  [Route("[controller]")]
  public class CommunityController : FailureController
  {
    readonly CommunityService _service;
    public CommunityController(CommunityService service) => _service = service;
    [HttpPost("join")]
    public async Task<IActionResult> JoinAsync((User user, int id) message) => await FailureProcess(async () =>
     {
       await _service.JoinAsync(message.user, message.id);
       return NoContent();
     });
    [HttpGet("search")]
    public async Task<IActionResult> SearchAsync((User user, string communityName) message) => await FailureProcess(async () =>
       {
         var community = await _service.SearchAsync(message.user, message.communityName);
         return Ok(community);
       });
    [HttpGet("select")]
    public async Task<IActionResult> SelectAsync(User user) => await FailureProcess(async () =>
    {
      var communities = await _service.SelectAsync(user);
      return Ok(communities);
    });
    [HttpDelete("quit")]
    public async Task<IActionResult> QuitAsync((User user, int communityId) message) => await FailureProcess(async () =>
     {
       await _service.QuitAsync(message.user, message.communityId);
       return NoContent();
     });

  }
}
